/**
 * 
 */
package front_shop;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * @author wx
 *
 */
public class registered_user extends JFrame implements ActionListener {

	/**
	 * @param args
	 */
	JLabel xm,hm,mm;
	JTextField name,num,password;

	JButton add;
	JPanel panel;
	Connection con;
    Statement sql; 
    ResultSet rs;
	registered_user(){
		
		
		 panel=new JPanel();
		 xm= new JLabel("会员姓名:");
		 hm= new JLabel("电话号码:");
		 mm= new JLabel("会员密码:");
		 name= new JTextField(20);
		 num= new JTextField(20);
		 password= new JTextField(20);
		 add=new JButton("确定");
		 
		panel.setLayout(null);
			this.add(panel);
		panel.add(xm);
		xm.setBounds(30,30,60,20);
		panel.add(name);
		name.setBounds(120,30,140,20);
		panel.add(hm);
		hm.setBounds(30,80,60,20);
		panel.add(num);
		num.setBounds(120,80,140,20);
		panel.add(mm);
		mm.setBounds(30,130,60,20);
		panel.add(password);
		password.setBounds(120,130,140,20);
		panel.add(add);
		add.setBounds(100,180,100,20);
		
		name.addActionListener(this);
		add.addActionListener(this);
		

		    try{  
		    Class.forName("com.mysql.jdbc.Driver");
		    }
		    catch(Exception e){
		       System.out.println("忘记把MySQL数据库的JDBC-数据库驱动程序复制到JDK的扩展目录中");
		    }
		    try { String uri= "jdbc:mysql://127.0.0.1:3306/javashop";
		          String user="root";
		          String ps="123";
		          con=DriverManager.getConnection(uri,user,ps);
		          sql=con.createStatement();
		    }
		    catch(SQLException e){ 
		          System.out.println(e);
		    }
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if (e.getSource()==add){
			 try { 
				 
				 String s="insert into usermanage(UserName,IDcard,Password,CardDate) values";
				 if (name.getText().equals("")||num.getText().equals("")||password.getText().equals(""))
					 JOptionPane.showMessageDialog(this,"所有均不能为空！！！","注册用户",JOptionPane.WARNING_MESSAGE);
				 else if (num.getText().length()!=11)
					 {JOptionPane.showMessageDialog(this,"号码位数不正确！！！","注册用户",JOptionPane.WARNING_MESSAGE);
					 num.requestFocus();}
				 else if (password.getText().length()!=6)
					 {JOptionPane.showMessageDialog(this,"需要设置六位密码！！！","注册用户",JOptionPane.WARNING_MESSAGE);
					 password.requestFocus();}
				 else 
					 { 
						 rs=sql.executeQuery("SELECT * FROM usermanage where IDcard=\""+num.getText()+"\"");
						 if( rs.next()==true){
							 JOptionPane.showMessageDialog(this,"该号码已被注册","注册用户",JOptionPane.WARNING_MESSAGE);
							 num.setText("");
							 password.setText("");
							 num.requestFocus();
						 }
						 else 
						 {	
								 Date nowTime=new Date();
								 String t=String.format("%tY/%<tm/%<td", nowTime);
								 s=s+"(\""+name.getText()+"\",\""+num.getText()+"\",\""+password.getText()+"\",\""+t+"\")";
								 int ok=sql.executeUpdate(s);     
								 JOptionPane.showMessageDialog(this,"注册成功~~","注册用户",JOptionPane.WARNING_MESSAGE);	
								 this.dispose();
								 con.close();
						 }
					 }

			 }
			 catch(SQLException a){ 
				 System.out.println(a);
			 }
		}
	}
/*	public static void main(String[] args) {
		// TODO Auto-generated method stub
		registered_user user=new registered_user();
		user.setTitle("注册新会员 ");
		user.setBounds(200,200,300,270);
		user.setVisible(true);
		user.validate();
		user.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	}*/

}

